package data.event;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import data.account.attendee.AttendeeAccount;

public class EventTable extends JTable
{
	private static final long serialVersionUID = 8792042683689370156L;
	
	private ArrayList<Event> events;
	public ArrayList<Event> events() { return events; }
	
	private Model eventModel;
	String[][] data;
	
	public EventTable(ArrayList<Event> eventList)
	{
		super(new String[0][4], new String[] { "Event", "Location", "Start", "End" });
		
		this.events = eventList;
		Collections.sort(eventList);
		data = new String[eventList.size()][4];
		for(int i = 0; i < eventList.size(); i++)
		{
			Event event = eventList.get(i);
			data[i][0] = event.name();
			data[i][1] = event.location();
			data[i][2] = event.startTimeText();
			data[i][3] = event.endTimeText();
		}
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setModel(eventModel = new Model());
		getColumnModel().getColumn(0).setHeaderValue("Event");
		getColumnModel().getColumn(1).setHeaderValue("Location");
		getColumnModel().getColumn(2).setHeaderValue("Start");
		getColumnModel().getColumn(3).setHeaderValue("End");
	}
	
	public void updateContents(int row, int column)
	{
		Collections.sort(events);
		data = new String[events.size()][4];
		for(int i = 0; i < events.size(); i++)
		{
			Event event = events.get(i);
			data[i][0] = event.name();
			data[i][1] = event.location();
			data[i][2] = event.startTimeText();
			data[i][3] = event.endTimeText();
		}
		eventModel.fireTableDataChanged();
		if(row != -1 && row < events.size() && column != -1 && column <= 3)
		{
			row = Math.max(0, Math.min(row, getRowCount() - 1));
			setRowSelectionInterval(row, row);
			setColumnSelectionInterval(column, column);
		}
		validate();
		repaint();
	}

	public Event getSelected()
	{
		int index = getSelectedRow();
		if(index != -1 && index < events.size())
		{
			return events.get(index);
		}
		return null;
	}
	
	public boolean canAddSelectedTo(EventTable eventTable)
	{
		Event event = getSelected();
		if(event != null)
		{
			for(Event eventB : eventTable.events)
				if(event.conflictsWith(eventB))
					return false;
			return true;
		}
		return false;
	}
	
	public void transferSelectedTo(EventTable eventTable)
	{
		Event event = getSelected();
		if(event != null && events.remove(event))
		{
			eventTable.events.add(event);
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			row = eventTable.getSelectedRow();
			column = eventTable.getSelectedColumn();
			eventTable.updateContents(row, column);
		}
	}

	public void setAttending(AttendeeAccount attendee)
	{
		for(Event event : events)
			if(!event.attendees().contains(attendee.userID()))
				event.attendees().add(attendee.userID());
	}
	
	public boolean removeEvent(Event event)
	{
		if(events.remove(event))
		{
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			return true;
		}
		return false;
	}
	
	public boolean addEvent(Event event)
	{
		if(!events.contains(event))
		{
			events.add(event);
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			return true;
		}
		return false;
	}
	
	private class Model extends AbstractTableModel
	{
		private static final long serialVersionUID = -6279807964207409321L;
		
		@Override public int getColumnCount() { return 4; }
		@Override public int getRowCount() { return data.length; }
		@Override public Object getValueAt(int row, int column) { return data[row][column]; }
	}
}